package com.cms.service;

import com.cms.mapper.CourseMapper;
import com.cms.pojo.Course;
import com.cms.pojo.Sc;
import com.cms.pojo.Teacher;
import com.cms.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.security.PublicKey;
import java.util.List;

public class CourseService {
    // 获取SqlSessionFactory对象
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    public List<Course> selectAll(int id) {
        // 获取SqlSession
        SqlSession sqlSession = factory.openSession();

        // 获取CourseMapper
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);

        // 调用方法
        List<Course> courses = mapper.selectAll(id);

        sqlSession.close();

        return courses;
    }

    public List<Course> selectAll2(int id) {
        // 获取SqlSession
        SqlSession sqlSession = factory.openSession();

        // 获取CourseMapper
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);

        // 调用方法
        List<Course> courses = mapper.selectAll2(id);

        sqlSession.close();

        return courses;
    }

    public List<Course> selectScore(int id) {
        // 获取SqlSession
        SqlSession sqlSession = factory.openSession();

        // 获取CourseMapper
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);

        // 调用方法
        List<Course> courses = mapper.selectScore(id);

        sqlSession.close();

        return courses;
    }

    public List<Course> myCourse(int id) {
        // 获取SqlSession
        SqlSession sqlSession = factory.openSession();

        // 获取CourseMapper
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);

        // 调用方法
        List<Course> courses = mapper.myCourse(id);

        sqlSession.close();

        return courses;
    }

    public void selectCourse(Sc sc) {
        // 获取SqlSession
        SqlSession sqlSession = factory.openSession();

        // 获取CourseMapper
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);

        // 调用方法
        mapper.selectCourse(sc);

        sqlSession.commit();
    }

    public void addSelected(int id) {
        // 获取SqlSession
        SqlSession sqlSession = factory.openSession();

        // 获取CourseMapper
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);

        // 调用方法
        mapper.addSelected(id);

        sqlSession.commit();
    }

    public Course selectById(int id) {
        // 获取SqlSession
        SqlSession sqlSession = factory.openSession();

        // 获取CourseMapper
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);

        // 调用方法
        Course course = mapper.selectById(id);

        sqlSession.close();

        return course;
    }

    public void cancelCourse(Sc sc) {
        // 获取SqlSession
        SqlSession sqlSession = factory.openSession();

        // 获取CourseMapper
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);

        // 调用方法
        mapper.cancelCourse(sc);

        sqlSession.commit();
    }

    public void decreaseSelected(int id) {
        // 获取SqlSession
        SqlSession sqlSession = factory.openSession();

        // 获取CourseMapper
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);

        // 调用方法
        mapper.decreaseSelected(id);

        sqlSession.commit();
    }

    public void writeScore(int s_id, int c_id, double score) {
        // 获取SqlSession
        SqlSession sqlSession = factory.openSession();

        // 获取CourseMapper
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);

        // 调用方法
        mapper.writeScore(s_id, c_id, score);

        sqlSession.commit();
    }

    public List<Course> allCourse() {
        // 获取SqlSession
        SqlSession sqlSession = factory.openSession();

        // 获取CourseMapper
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);

        // 调用方法
        List<Course> courses = mapper.allCourse();

        sqlSession.close();

        return courses;
    }

    public void deleteCourse(int id) {
        // 获取SqlSession
        SqlSession sqlSession = factory.openSession();

        // 获取CourseMapper
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);

        // 调用方法
        mapper.deleteSelectedCourse(id);
        mapper.deleteCourse(id);

        sqlSession.commit();
    }

    public int selectIdByName(String t_name) {
        // 获取SqlSession
        SqlSession sqlSession = factory.openSession();

        // 获取CourseMapper
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);

        // 调用方法
        int id = mapper.selectIdByName(t_name);

        sqlSession.close();

        return id;
    }

    public void addCourse(Course course) {
        // 获取SqlSession
        SqlSession sqlSession = factory.openSession();

        // 获取CourseMapper
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);

        // 调用方法
        mapper.addCourse(course);

        sqlSession.commit();
    }
}
